﻿Public Class frmInputNoteSeries
    Private serieDA As New DSVaultingTableAdapters.khr_sheet_seriesTableAdapter
    Private seriesDt As New DataTable
    Private action As String
    Private actType As String() = {"NONE", "INSERT", "UPDATE"}
    Private slcVal As Long

    Private Sub frmInputNoteSeries_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DSVaulting.khr_sheets' table. You can move, or remove it, as needed.
        Me.Khr_sheetsTableAdapter.Fill(Me.DSVaulting.khr_sheets)
        seriesDt = serieDA.GetDataBySheetsID
        updateDGV()
    End Sub

    Private Sub frmInputNoteSeries_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        frmSetting.Enabled = True
    End Sub

    Private Sub uxInputNewSeriesbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxInputNewSeriesbtn.Click
        uxNoteSeriesTbx.Text = ""
        uxNoteSeriesTbx.Enabled = True
        action = actType(1)
    End Sub

    Private Sub uxEditNotebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxEditNotebtn.Click
        If uxNoteSeriesDGV.SelectedRows.Count > 0 Then
            uxNoteSeriesTbx.Enabled = True
            uxNoteTypeCbx.Text = uxNoteSeriesDGV.SelectedRows.Item(0).Cells("NoteType").Value
            uxNoteSeriesTbx.Text = uxNoteSeriesDGV.SelectedRows.Item(0).Cells("NoteSeries").Value
            If Not uxNoteSeriesTbx.Text = "" Then
                slcVal = uxNoteSeriesTbx.Text
                action = actType(2)
            End If
        End If
    End Sub

    Private Sub uxInputNoteSavebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxInputNoteSavebtn.Click
        Dim year As String = uxNoteSeriesTbx.Text
        Dim sheetId As Integer = Integer.Parse(uxNoteTypeCbx.SelectedValue)
        If Not year = "" And action = actType(1) Then
            serieDA.Insert(sheetId, year)
            Me.updateDGV()
            uxNoteSeriesTbx.Clear()
        ElseIf Not year = "" And action = actType(2) Then
            If MessageBox.Show("តើអ្នកពិតជាចងរក្សាទុកព័ត៌មានដែលបានកែប្រែមែនទេ?", "សារបញ្ជាក់ពីការកែប្រែ", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
                Dim oldSeriesId As Long = uxNoteSeriesDGV.SelectedRows.Item(0).Cells(0).Value
                Dim oldSheetId As Long = uxNoteSeriesDGV.SelectedRows.Item(0).Cells(1).Value
                Dim oldYear As Integer = uxNoteSeriesDGV.SelectedRows.Item(0).Cells(2).Value
                serieDA.Update(sheetId, year, oldSeriesId, oldSheetId)
                Me.updateDGV()
            End If
        Else
            MsgBox("Please input some value.")
        End If
    End Sub

    Private Sub updateDGV()
        uxNoteSeriesDGV.DataSource = serieDA.GetDataBySheetsID
    End Sub

    Private Sub uxCancelBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxCancelBtn.Click
        frmSetting.Enabled = True
        Me.Close()
    End Sub

    Private Sub uxDeleteSeriesBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxDeleteSeriesBtn.Click
        If MessageBox.Show("តើអ្នកពិតជាចង់លុបចោលទិន្នន័យនេះមែនទេ?", "សារបញ្ជាក់ពីការលុបចោល", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
            Dim oldId As Long = uxNoteSeriesDGV.SelectedRows.Item(0).Cells(0).Value
            Dim oldSeriesId As Long = uxNoteSeriesDGV.SelectedRows.Item(0).Cells(0).Value
            Dim oldSheetId As Long = uxNoteSeriesDGV.SelectedRows.Item(0).Cells(1).Value
            Dim oldYear As Integer = uxNoteSeriesDGV.SelectedRows.Item(0).Cells(2).Value
            serieDA.Delete(oldSeriesId, oldSheetId)
            Me.updateDGV()
        End If
    End Sub

    Private Sub uxNoteSeriesTbx_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles uxNoteSeriesTbx.KeyPress
        If (e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter)) Then
            uxInputNoteSavebtn_Click(sender, e)
        End If
    End Sub

    Private Sub uxNoteSeriesDGV_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles uxNoteSeriesDGV.CellDoubleClick
        uxEditNotebtn_Click(sender, e)
    End Sub
End Class